<template>
  <div class="atme-page">
    <!-- 头部 -->
    <Navbar title="@我的" placeholder bgColor="#161622">
      <template #left>
        <div class="back-btn" @click="handleBack">
          <Icon name="zuojiantou" color="#fff" size="18"></Icon>
        </div>
      </template>
    </Navbar>

    <!-- @我列表 -->
    <div class="atme-list">
      <div v-for="item in atMeList" :key="item.userId" class="atme-item">
        <!-- 头像 -->
        <div class="avatar-container">
          <img :src="item.avatar" alt="头像" class="avatar" />
        </div>

        <!-- 内容 -->
        <div class="content">
          <div class="nickname">@{{ item.userNickname }}</div>
          <div class="mention">{{ item.commentContent }}</div>
          <div class="time">提到了你 {{ item.time }}</div>
        </div>

        <!-- 右侧图片 -->
        <div class="right-content">
          <img :src="item.videoCover" alt="作品封面" class="thumbnail" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Navbar from '@/components/common/Navbar.vue'
import Icon from '@/components/common/Icon.vue'
import { formatTimeAgo } from '@/utils/time'
import { Toast } from '@/utils/showtoast'
import { useUserStore } from '@/stores/userinfo'
import { useRouter } from 'vue-router'
import { atMyPageAPI } from '@/api/message' // 假设 API 已存在

const userStore = useUserStore()
const { userId } = userStore.userInfo
const router = useRouter()

// 数据
const atMeList = ref([])

// 获取 @我 列表
const getAtMeList = async () => {
  try {
    const res = await atMyPageAPI(userId)
    if (res.code === 200) {
      res.data.forEach(item => {
        item.avatar = 'http://43.138.15.137:3000' + item.userAvatar
        item.time = formatTimeAgo(item.createdAt)
      })
      atMeList.value = res.data
    }
  } catch (error) {
    console.error('获取 @我 列表失败:', error)
    Toast('加载失败，请重试', { position: 'bottom' })
  }
}

onMounted(() => {
  getAtMeList()
})

// 返回
const handleBack = () => router.back()
</script>

<style lang="scss" scoped>
.atme-page {
  width: 100%;
  min-height: 100vh;
  background-color: #1d1f2a;
  color: #fff;
}

.atme-list {
  padding: 16px;
}

.atme-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #2a2a2a;

  &:last-child {
    border-bottom: none;
    padding-bottom: 40px;
  }

  .avatar-container {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 12px;
  }

  .content {
    flex: 1;
    min-width: 0;

    .nickname {
      font-size: 16px;
      font-weight: 500;
      margin: 0;
    }

    .mention {
      font-size: 14px;
      color: #ccc;
      margin: 4px 0;
      word-break: break-word;
    }

    .time {
      font-size: 12px;
      color: #999;
    }
  }

  .right-content {
    flex-shrink: 0;
    text-align: right;
    margin-left: auto;

    .thumbnail {
      width: 60px;
      height: 60px;
      border: 1px solid #ddd;
      border-radius: 4px;
      object-fit: cover;
    }
  }
}
</style>